package Servlets;

import PackDatos.GestionBD;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class srv_registraNotas extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        Connection cn = null;
        try {
            //Obtengo los datos
            int alumno = Integer.parseInt(request.getParameter("alumno"));
            int curso = Integer.parseInt(request.getParameter("curso"));
            int nota1 = Integer.parseInt(request.getParameter("nota1"));
            int nota2 = Integer.parseInt(request.getParameter("nota2"));
            int nota3 = Integer.parseInt(request.getParameter("nota3"));
            int nota4 = Integer.parseInt(request.getParameter("nota4"));
            int eparcial = Integer.parseInt(request.getParameter("eparcial"));
            int efinal = Integer.parseInt(request.getParameter("efinal"));
            System.out.println("Datos atrapados: " + alumno + curso + nota1 + nota2 + nota3 + nota4 + eparcial + efinal);
            // Proceso
            //obtener la nota minima
            int min=Math.min(nota1, Math.min(nota2, Math.min(nota3, nota4)));
            int prompractica=(nota1+nota2+nota3+nota4-min)/3;
            int promfinal=(prompractica+eparcial+2*efinal)/4;
            GestionBD og = new GestionBD();
            cn = og.conectar();
            String sql = "update nota set not_practica1=?,not_practica2=?,not_practica3=?,not_practica4=?"
                    + ",not_prompractica=?,not_examparcial=?,not_examfinal=?,not_promfinal=? where id_alumno="+alumno+" and id_curso="+curso;
            PreparedStatement  pstm = cn.prepareStatement(sql);
            pstm.setInt(1,nota1);
            pstm.setInt(2,nota2);
            pstm.setInt(3,nota3);
            pstm.setInt(4,nota4);
            pstm.setInt(5,prompractica);
            pstm.setInt(6,eparcial);
            pstm.setInt(7,efinal);
            pstm.setInt(8,promfinal);
            pstm.executeUpdate();
            request.setAttribute("mensaje", "Notas registradas correctamente!");
        } catch (Exception ex) {
            request.setAttribute("mensaje", ex.getMessage());
        }
        RequestDispatcher rd = request.getRequestDispatcher("registrarNotas.jsp");
        rd.forward(request, response);
    }
}
